//
//  EditStaffRoleController.m
//  QRMaster
//
//  Created by myApple on 16/5/17.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "EditStaffRoleController.h"
#import "RequestNetWork.h"
#import "StaffLogic.h"

@interface EditStaffRoleController ()<UITableViewDataSource,UITableViewDelegate>
{
    StaffModel * editModel;
    UITableView * editStaffRoleTabelView;
    UIImageView * submitImageView;
    NSArray * roleArray;
    UILabel * submitLabel;
    NSString * roleString ;
}

@end

@implementation EditStaffRoleController

-(id)initWithModel:(StaffModel *)model
{
    self = [super init];
    if (self) {
        editModel = model;
        roleString = editModel.StaffRole;
        roleArray = @[NSLocalizedString(@"subManagement", nil),NSLocalizedString(@"general", nil)];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"RoleManagment", nil);
    self.view.backgroundColor = UIColorFromRGB(0xebebeb);
    [self prepareNavBtn];
    [self prepareTabelView];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    submitImageView.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    submitImageView.hidden = YES;
}

-(void)prepareTabelView
{
    float x = 0;
    float y = 5;
    float height = UI_SCREEN_HEIGHT - 64 - y;
    float width = UI_SCREEN_WIDTH;
    
    editStaffRoleTabelView = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStyleGrouped];
    editStaffRoleTabelView.backgroundColor = UIColorFromRGB(0xebebeb);
    editStaffRoleTabelView.delegate = self;
    editStaffRoleTabelView.dataSource = self;
    editStaffRoleTabelView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:editStaffRoleTabelView];
}


-(void)prepareNavBtn
{
    submitImageView = [[UIImageView alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH - 60, 2, 50, 40)];
    submitImageView.userInteractionEnabled = NO;
    UITapGestureRecognizer * submitTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(submit)];
    [submitImageView addGestureRecognizer:submitTap];
    [self.navigationController.navigationBar addSubview:submitImageView];
    
    submitLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 50, 20)];
    submitLabel.text = NSLocalizedString(@"authenSubmit",@"完成");
    submitLabel.textColor = UIColorFromRGBAndA(0x7dc234, 0.5);
    submitLabel.font = [UIFont systemFontOfSize:15.0];
    [submitImageView addSubview:submitLabel];
}

#pragma mark ------------ 提交修改
-(void)submit
{
    [self.view endEditing:YES];
    if (editModel.userName.length > 20||editModel.userName.length < 1)
    {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil)];
        return;
    }
    __weak EditStaffRoleController * weakSelf = self;
    NSMutableDictionary * saveDict = [[NSMutableDictionary alloc]init];
    [saveDict setValue:UID forKey:@"id"];
    [saveDict setValue:TOKEN forKey:@"token"];
    [saveDict setValue:editModel.userName forKey:@"name"];
    [saveDict setValue:roleString forKey:@"role"];
    [saveDict setValue:editModel.sex forKey:@"gender"];
    [saveDict setValue:[QRSingleton sharedQRSingleton].communityId forKey:@"communityId"];
    [saveDict setValue:editModel.userID forKey:@"memberId"];
    
    if([self getTimestamp:[QRSingleton sharedQRSingleton].communityId  timeKey:@"member_timestamp"] != nil)
    {
        [saveDict setValue:[self getTimestamp:[QRSingleton sharedQRSingleton].communityId  timeKey:@"member_timestamp"] forKey:@"timestamp"];
    }
    else
    {
        [saveDict setValue:@"" forKey:@"timestamp"];
    }
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(EDITADDINFO) params:saveDict block:^(id downLoadData, id error) {
        NSDictionary * cache = [downLoadData objectForKey:@"cache"];
        NSDictionary * data = [downLoadData objectForKey:@"data"];
        if(cache != nil)
        {
            editModel.StaffRole = roleString;
            if([@"0" isEqualToString:[NSString stringWithFormat:@"%@",[cache objectForKey:@"status"]]]==YES)
            {
                [self updateTimestamp:[QRSingleton sharedQRSingleton].communityId  timeKey:@"member_timestamp" timeKeyValue:[cache objectForKey:@"timestamp"]];
            }
            
            if (data != nil)
            {
                
                [PromptInfoView showMessage:NSLocalizedString(@"editSuccess", nil)];
                StaffLogic * staffDataBase = [[StaffLogic alloc]init];
                [staffDataBase updateStaffData:editModel communityId:[QRSingleton sharedQRSingleton].communityId ];
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
            if ([weakSelf editStaffBlock]) {
                weakSelf.editStaffBlock(editModel);
            }
        }
        else
        {
            
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
        
    }];
}

#pragma mark --------tableviewdelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return roleArray.count;
}

//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return NSLocalizedString(@"le", nil); //ModifyRole
//}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *header = @"customHeader";
    UITableViewHeaderFooterView *vHeader;
    vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
    
    if (!vHeader) {
        vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
    }
    vHeader.textLabel.text = NSLocalizedString(@"ModifyRole", nil);
    return vHeader;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * identityId = @"cellId";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identityId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identityId];
    }
    
    NSString * titleString = roleArray[indexPath.row];
    cell.textLabel.text = titleString;
    cell.textLabel.font = [UIFont systemFontOfSize:16.0];
    if ([@"2" isEqualToString:roleString] && indexPath.row == 0) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else if([@"3"isEqualToString:roleString] && indexPath.row == 1)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        roleString = @"2";
    }
    else
    {
        roleString = @"3";
    }
    [editStaffRoleTabelView reloadData];
    [self judueSubmit];
}

-(void)judueSubmit
{
    if ([roleString isEqualToString:editModel.StaffRole]) {
        submitLabel.textColor = UIColorFromRGBAndA(0x7dc234, 0.5);
        submitImageView.userInteractionEnabled = NO;
    }
    else
    {
        submitLabel.textColor = UIColorFromRGBAndA(0x7dc234, 1);
        submitImageView.userInteractionEnabled = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
